<template>
  <div class="closing">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="isClosingDialog=true">{{ $t('market.filter') }}<!--过滤--></el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.period') }}<!--期间--></span>
        <el-input v-model="period" size="mini" disabled />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        get-row-keys="fuuId"
        default-expand-all
        serial
        sortable
        :tree-props="{children: 'childList', hasChildren: 'hasChild'}"
      >
        <el-table-column :label="$t('market.subjectName')" prop="subjectName" width="150" header-align="center" align="center" />
        <el-table-column :label="$t('market.subjectNum')" align="center">
          <template slot-scope="scope">
            <el-link type="primary" @click="pushView(scope.row, scope.$index)">{{ scope.row.fnumber }}</el-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('market.dimensionString')" prop="dimensionString" align="center" />
        <el-table-column :label="$t('market.accountingyear')" prop="fiscalYear" align="center" />
        <el-table-column :label="$t('market.period')" prop="period" align="center" />
        <el-table-column :label="$t('market.digest')" prop="description" align="center" />
        <el-table-column :label="$t('market.debtor')" prop="amountIn" align="center" />
        <el-table-column :label="$t('market.credit')" prop="amountOut" align="center" />
        <el-table-column :label="$t('market.direction')" prop="situation" align="center" />
        <el-table-column :label="$t('market.money')" prop="amount" align="center" />
      </jc-table>
    </div>
    <!-- 过滤 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.filter')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="600px"
      center
    >
      <div>
        <el-form :model="subDetails" size="mini" label-width="100px">
          <el-form-item :label="$t('market.startMonth')">
            <el-date-picker
              v-model="subDetails.startMonth"
              type="month"
              value-format="yyyy-MM"
              :placeholder="$t('market.startMonth')"
            />
          </el-form-item>
          <el-form-item :label="$t('market.endMonth')">
            <el-date-picker
              v-model="subDetails.endMonth"
              type="month"
              value-format="yyyy-MM"
              :placeholder="$t('market.endMonth')"
            />
          </el-form-item>
          <el-form-item :label="$t('market.levelKm')">
            <el-select v-model="subDetails.level">
              <el-option label="全部" value="" />
              <el-option v-for="item in 4" :key="item" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.startFnumber')">
            <el-input v-model.trim="subDetails.startFnumber" style="width: 220px">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="openSubList(1)"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.endFnumber')">
            <el-input v-model.trim="subDetails.endFnumber" style="width: 220px">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="openSubList(2)"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.dimension')">
            <el-input v-model.trim="subDetails.dimensionString " style="width: 220px" />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="subDetails.findStatus">{{ $t('market.findStatus') }}</el-checkbox>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isClosingDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="initialCon">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
    <!--    科目列表-->
    <subjectList v-if="isSubListVisibled" msg="2" @subject="sublistData" />
  </div>
</template>

<script>
import {
  generalLedger
} from '@/api/financial/subject'
import subjectList from '@/views/financial/voucherTemplate/components/subjectList'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'

export default {
  name: 'GeneralLedger',
  components: {
    subjectList
  },
  data() {
    return {
      subDetails: {
        startYear: '',
        startMonth: '',
        endYear: '',
        endMonth: '',
        level: 1,
        balanceZero: true,
        startFnumber: '',
        endFnumber: ''
      },
      subject: 1,
      expandKeys: [], // 展开行
      toExcelData: [],
      period: '',
      loading: false,
      isClosingDialog: false,
      isSubListVisibled: false,
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      tableHeader: [],
      start: '',
      end: ''
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 200px)' : '73vh'
  },
  methods: {
    // 过滤确认
    initialCon() {
      this.isClosingDialog = false
      const { startMonth, endMonth } = this.subDetails
      this.period = startMonth + '--' + endMonth
      this.handleGetUntreated()
    },
    // 获取科目余额列表数据
    async handleGetUntreated() {
      const { startMonth, endMonth, level, startFnumber, dimensionString, endFnumber, findStatus, balanceZero } = this.subDetails
      const DATA = {
        startFnumber, endFnumber, level, dimensionString,
        findStatus: findStatus ? 'B' : '',
        balanceZero: balanceZero ? 1 : 0,
        startYear: startMonth.split('-')[0],
        startMonth: startMonth.split('-')[1],
        endYear: endMonth.split('-')[0],
        endMonth: endMonth.split('-')[1]
      }
      const RES = await generalLedger(DATA)
      if (RES.code === 0) {
        this.start = RES.data.start
        this.end = RES.data.end
        this.tableData = RES.data.List
        this.toExcelData = RES.data.Excel
      } else {
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 打开科目
    openSubList(ev) {
      this.subject = ev
      this.isSubListVisibled = true
    },
    // 科目数据
    sublistData(item) {
      if (item.fsubjectId) {
        if (this.subject === 1) {
          this.subDetails.startFnumber = item.fnumber
        } else {
          this.subDetails.endFnumber = item.fnumber
        }
      }
      this.isSubListVisibled = false
    },
    // 跳转详情
    pushView(ev, index) {
      const item = this.tableData[index + 1] // 本期 amountIn amountOut
      const item2 = this.tableData[index + 2] // 本年
      this.$router.push({ path: `/subsidiaryLedgers/${ev.amount}`,
        query: {
          periodAmountIn: item.amountIn,
          periodAmountOut: item.amountOut,
          yearAmountIn: item2.amountIn,
          yearAmountOut: item2.amountOut,
          dimensionString: ev.dimensionString,
          subjectId: ev.subjectId,
          subjectName: ev.subjectName,
          fiscalYear: ev.fiscalYear,
          period: ev.period,
          amount: ev.amount,
          situation: ev.situation
        }
      })
    },
    // 搜索
    handleQuerySonClass() {
      this.currentPage = 1
      this.handleGetUntreated()
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = [
          '科目编码', '科目名称', '核算维度', '会计年度', '期间', '摘要', '借方', '贷方', '方向', '余额'
        ]
        const filterVal = [
          'subjectName', 'fnumber', 'dimensionString', 'fiscalYear', 'period', 'description', 'amountIn', 'amountOut',
          'situation', 'amount'
        ]
        if (this.toExcelData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          const list = this.toExcelData
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '总分类账')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
